package important.demo;
import java.util.ArrayList;

import presence.demo.PresenceMainActivity;
import presence.demo.R;
import database.demo.DataBaseWrapperImportant;
import database.demo.DataBaseWrapperPassengers;
import database.demo.ImportantContant;
import database.demo.ImportantOperations;
import database.demo.Passenger;
import database.demo.PassengersOperations;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;


/**
 * saving important contact's details
 */
public class ImportantListMainActivity extends Activity {
private ImportantOperations importantDBoperation;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.important_list);
		importantDBoperation = new ImportantOperations(this);
		importantDBoperation.open();
		populateImportantList();		
	}
	
	//get all important from DB
	private void populateImportantList() {
		
		final Cursor cursor = importantDBoperation.getAllImportant();
		
		// Allow activity to manage lifetime of the cursor.
		// DEPRECATED! Runs on the UI thread, OK for small/short queries.
		startManagingCursor(cursor);
		
		// Setup mapping from cursor to view fields:
		String[] fromFieldNames = new String[] 
				{DataBaseWrapperImportant.IMPORTANT_ID,DataBaseWrapperImportant.IMPORTANT_NAME,
				DataBaseWrapperImportant.IMPORTANT_ROLE};
				
		int[] toViewIDs = new int[]
				{R.id.idText,R.id.dname_text, R.id.textFirst};
		
		// Create adapter to may columns of the DB onto elemesnt in the UI.
		SimpleCursorAdapter myCursorAdapter = 
				new SimpleCursorAdapter(
						this,		// Context
						R.layout.important_item_view,	// Row layout template
						cursor,					// cursor (set of DB records to map)
						fromFieldNames,			// DB Column names
						toViewIDs				// View IDs to put information in
						);
		
		
		// Set the adapter and listener for the list view
		ListView myList = (ListView) findViewById(R.id.important_list);
		
		
		myList.setOnItemClickListener(new OnItemClickListener() {
			  @Override
			  public void onItemClick(AdapterView<?> parent, View view,
			    int position, long id) {
			    //Toast.makeText(getApplicationContext(),"Click ListItem Number " + position, Toast.LENGTH_LONG).show();
				cursor.moveToFirst();
				cursor.moveToPosition(position);
				String tmpID = cursor.getString(0).toString();
				//Toast.makeText(getApplicationContext(),tmpID, Toast.LENGTH_LONG).show();
				
				Intent intent =new Intent(important.demo.ImportantListMainActivity.this,SpecificImportantMainActivity.class);
			    intent.putExtra("tmpID", tmpID);
				startActivity(intent);		
			  }
			});
		myList.setAdapter(myCursorAdapter);
		int count = myList.getAdapter().getCount();
		if (count <1){
			Toast.makeText(ImportantListMainActivity.this,
					"No available contacts", Toast.LENGTH_SHORT).show();
		}
		
	}

	@Override
	protected void onResume() {
	   super.onResume();
	   this.onCreate(null);
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.important_list, menu);
        return true;
    }
	
	public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.item1:
        	Intent newPass=new Intent(important.demo.ImportantListMainActivity.this,NewImportantMainActivity.class);
		    startActivity(newPass);
		    return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
	
	
	
				
	@Override
	protected void onDestroy() {
		super.onDestroy();	
		importantDBoperation.close();
	}
}
